﻿using LuDragon.Data.Entities;
using LuDragon.IService.System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LuDragon.Service.System
{
    /// <summary>
    /// 系统设置服务类
    /// </summary>
    public class NavigationService : BaseService<Sys_Navigation>, INavigationService
    {

        /// <summary>
        /// 获取导航列表
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns></returns>
        public List<Sys_Navigation> GetNavigationList(int roleId)
        {
            IRoleService roleService = new RoleSerivce();
            var role = roleService.GetRole(roleId);
            var navList = new List<Sys_Navigation>();
            if (roleId == 1)//1表示超级管理员
            {
                navList = GetAll().ToList();
            }
            else
            {
                //navList = GetBySql<Sys_Navigation>(SqlStr.GetNavigation, new { ids = role.NavIds?.Split(',') }).ToList();
                navList = GetAll().Where(a => role.NavIds.Contains(a.NavId.ToString())).ToList();
            }
            return navList.OrderBy(s => s.Sort).ToList();
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Sys_Navigation Find(int id)
        {
            return GetModel(a => a.NavId == id);
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Sys_Navigation Save(Sys_Navigation model)
        {
            if (model == null)
            {
                throw new Exception("实体参数为Null");
            }
            if (model.NavId > 0)
            {
                Update(model);
                return model;
            }
            else
            {
                return Add(model);
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Del(int id)
        {
            return Delete(GetModel(a => a.NavId == id));
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Del(Sys_Navigation model)
        {
            return Delete(model);
        }
        /// <summary>
        /// 菜单保存排序
        /// </summary>
        /// <param name="idArr"></param>
        /// <param name="sortArr"></param>
        public bool SaveSort(string[] idArr, string[] sortArr)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < idArr.Length; i++)
            {
                sb.AppendFormat(" update Sys_Navigation set Sort={0} where NavId={1}; ", sortArr[i], idArr[i]);
            }
            if (ExcuteSql(sb.ToString()) > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }
    }
}
